<?php
/**
 +------------------------------------------------------------------------------
 * @author   heihei2202 <admin@sameus.com>
 * @version    2012-05-15 04:59:14Z heihei2202 $
 +------------------------------------------------------------------------------
 */
// 本类由系统自动生成，仅供测试用途
class GroupAction extends CenterAction {
    
   public function index(){
//        
//        $groupview=D('GroupsView');
//        //$data['uid']='1';
//        $data=$groupview->where($data)->field('gid,group_sort,group_name,group_intro,group_num,uid,user_name,user_psd,user_info,state')->select();
//        $this->assign('group_list',$data);
//        
//       $this->display('Index:grouplist');
       
    }  
    
     public function audit(){
        $groupview=D('GroupsView');
        //$data['uid']='1';
        $data=$groupview->where($data)->field('gid,group_sort,group_name,group_logo,group_intro,group_num,uid,user_name,user_psd,user_info,state')->select();
        $this->assign('group_list',$data);
        
       $this->display('audit');
   }
//     public function audit_insert(){
//         
//          $data=$model->create();
//          if($id=$model->add($data)){
//              $this->success('成功');
//          }else{
//             $this->error('失败');
//          }
//     }
  
  
    
    public function creategroup(){
      $this->display('Index:creategroup');
    }
     public function group_insert(){
         $group=D('Groups');
         $key=M('Keyword');
         $relation=M('keyword_relation');
         $group_user=M('Groups_user');
         
         if($data=$group->create()){
              $data['user_id']= $_SESSION['uid'];
              $data['user_name']= $_SESSION['uname'];
              $data['create_time']=date('Y-m-d H:i:m',time());
              $data['state']=0;
             if($gid=$group->add($data)){
                  $datas['group_id']=$gid; 	
                  $datas['user_id']= $_SESSION['uid']; //实验数据为1
                  $datas['state']=1; 
                  if($guid=$group_user->add($datas)){//设置管理员
                     if($_POST['keywords']!=null){
                          $keyword=$_POST['keywords'];
                     }
                      foreach($keyword as $value){
                           $info['key_name']=$value;
                          if($keylist=$key->where($info)->find()){
                             $relate['keyword_id']=$keylist['id']; 
                           }  elseif ($keyid=$key->add($info)){
                                 $relate['keyword_id']=$keyid;
                           }
                           $relate['main_id']=$gid;
                           $relate['type']=1;//type,1为社群,2为文章
                           $relation->add($relate); 
                      }
                  }
                  $this->assign('gid',$gid);
                 $this->display('Index:creategroup_logo');
             }else{
                 $this->error('失败');
             }
         }else{
             $this->error($group->getError());
         }
         
     }
      public function edit_groupinfo(){
        $this->display('edit_groupinfo');
   }
   
     //社群信息修改保存
     public function group_save(){
          $group=D('Groups');
          if($data=$group->create()){
              $group->save($data) ? $this->success('成功'):  $this->error('失败') ; 
          }else{
             $this->error($group->getError() );
          }
     }
     public function up_group(){
           $this->display('Index:creategroup_logo');
     }
     /*
      * 群组logo上传
      */
      public function grouplogo_upload(){
          if (!empty($_FILES)) {
            //如果有文件上传 上传附件
             $image=$this->_upload();
            //$this->forward();
              $model = M('Groups');
                //保存当前数据对象
                $data['gid'] = $_POST['gid'];
                $data['group_logo'] = $image;
                $list = $model->save($data);
                if ($list !== false) {
                    $this->success('上传图片成功！');
                } else {
                    $this->error('上传图片失败!');
                }
          }
     }
       /*
      * 群组logo修改
      */
      public function grouplogo_edit(){
          if (!empty($_FILES)) {
            //如果有文件上传 上传附件
             $image=$this->_upload();
            //$this->forward();
              $model = M('Groups');
                //保存当前数据对象
                $data['gid'] = $_GET['gid'];
                $data['group_logo'] = $image;
                $list = $model->save($data);
                $this->ajaxReturn($image,'ok',1);
          }
     }
     // 文件上传
    protected function _upload() {
        import("@.ORG.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath = './Public/Uploads/GroupLogo/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = '@.ORG.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '120';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '120';
        //设置上传文件规则
        //$upload->saveRule = uniqid;
        $upload->saveRule = 'glogo'.$_GET['gid'];
        //删除原图
        $upload->thumbRemoveOrigin = true;
       // 存在同名是否覆盖
       $upload-> uploadReplace = true;
         
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            import("@.ORG.Image");
            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
            Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], '../Public/Images/logo2.png');
            $_POST['image'] = $uploadList[0]['savename'];
       }
       return $_POST['image'];
      
        
    }

     public function audit_article(){
         
         $deliver=D('DeliverView');
         $data['for_id']=$_GET['gid'];
         $deliverlist=$deliver->where($data)->select();
          // dump($deliverlist);
         $this->assign('deliverlist',$deliverlist);
         $this->display('Group:audit_article');
         
     }
}